from hp_shared.base import _console, _print, _Object
from hp_shared.storage import clipboard
import os
import time
import pyautogui as pg

__filename = __file__
__dirname = os.path.dirname(os.path.abspath(__file__))
# 配置
# _Object 类使对象同时支持.和[]方式访问
wa_windows_config = _Object({
    'wx': 'wa-windows-wx.png',
    'wxSearch': 'wa-windows-wx-search.png',
    'wxSearchContent': 'anwei609',
    'wxSend': ['enter'],
    'minify': True,
    'pgPause': 1,
})
hp_windows_config = _Object({
    'wx': 'hp-windows-wx.png',
    'wxSearch': 'hp-windows-wx-search.png',
    'wxSearchContent': 'Feawaris',
    'wxSend': ['ctrl', 'enter'],
    'minify': False,
    'pgPause': 0.3,
})


def send_message(config: _Object) -> None:
    # 设置
    pg.FAILSAFE = True  # 设置失败安全模式
    pg.PAUSE = config.pgPause  # 设置操作之间的暂停时间(秒)

    # 最小化
    if config.minify:
        pg.hotkey('win', 'm')

    # 获取微信图标位置并点击
    location1 = pg.locateOnScreen(os.path.join(__dirname, config.wx), confidence=0.7)
    _print('微信图标位置', location1)  # _print 同 _console.log，根据习惯选用
    pg.doubleClick(location1)

    # 获取微信搜索框位置并搜索微信号
    location2 = pg.locateOnScreen(os.path.join(__dirname, config.wxSearch), confidence=0.7)
    _print('微信搜索框位置', location2)
    pg.doubleClick(location2)

    # 搜索内容
    pg.typewrite(config.wxSearchContent)
    time.sleep(0.3)

    # 聚焦输入框
    pg.press('enter')

    # 打开文案
    with open(os.path.join(__dirname, '语录.txt'), 'r', encoding='utf-8') as f:
        lists = f.readlines()

    # 批量发送
    for i in lists:
        # 内容
        i = i.strip()
        _print(i)
        # 复制
        clipboard.copySync(i)
        # 粘贴
        pg.hotkey('ctrl', 'v')
        # 发送
        # pg.hotkey(*config.wxSend)


if __name__ == '__main__':
    pass
    send_message(hp_windows_config)
    # send_message(wa_windows_config)
